import { observable, action } from 'mobx-miniprogram';

export const store = observable({
  carList: wx.getStorageSync('carList')  || [],

  addCarList: action(function(payload) {
    const item = this.carList.find(v => v.id === payload.id);
    if(item) {
      item.count++;
    } else {
      this.carList.push(payload)
    }
    this.carList = [...this.carList]
    wx.setStorageSync('carList', this.carList)
  }),
  changeCount: action(function (payload) {
    const item = this.carList.find(v => v.id === payload.id);
    if(item) item.count = payload.count

    this.carList = [...this.carList];
    wx.setStorageSync('carList', this.carList)
  }),
  changeState: action(function(payload) {
    const item = this.carList.find(v => v.id === payload.id);
    if(item) {
      item.state = !item.state
    }
    this.carList = [...this.carList]
    wx.setStorageSync('carList', this.carList)
  })
})

